package com.leyun68.server.config.swagger;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration // 加入 spring 容器中
@EnableSwagger2 // 开启 Swagger2
@EnableSwaggerBootstrapUI // 第三方 ui 框架 ToDo 这个访问根目录下的 doc.html
public class SwaggerConfig {
    // 是否试生产模式
    @Value("${production}")
    private boolean production;

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 如果 false swagger 不会开启
                .enable(production)
                // 需要配合下边的链式表达式一同使用
                .select()
                // 扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.leyun68.server"))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
    }

    // 作者信息
    Contact contact = new Contact("卢文凯 & 柳伊林", "http://bg.leyun68.com", "a1518967800@163.com");

    // 配置 swagger 信息 = apiInfo
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("乐云 办公系统")
                .description("卢文凯 & 柳伊林 前后端分离项目")
                .termsOfServiceUrl("http://bbpt.leyun68.com/")
                .contact(contact)
                .version("1.0")
                .build();
    }

    /**
     * 全局 token 配置
     */
    private List<ApiKey> securitySchemes() {
        // 设置请求头信息
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "Header");
        result.add(apiKey);
        return result;
    }

    private List<SecurityContext> securityContexts() {
        // 设置需要登录认证的路径
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/"));
        return result;
    }

    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization", authorizationScopes));
        return result;
    }
}
